package com.carlinfo.back.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.carlinfo.userscenter.users.pojo.AAdmins;

public class AuthFilter implements Filter
{
	private Logger logger = LogManager.getLogger();
	@Override
	public void destroy()
	{
		this.logger.info("过滤器销毁了");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain)
			throws IOException, ServletException
	{
		System.out.println("=======拦截器工作了啊========");
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		HttpSession session = request.getSession();
		AAdmins admins =  (AAdmins) session.getAttribute("admins");
		if(admins!=null)
		{
			this.logger.info("我是拦截器id"+admins.getId());
			filterChain.doFilter(req, res);
		}else
		{
			String info = "对不起,您输入的信息有误!";
			session.setAttribute("info", info);
			String path = request.getContextPath() + "/NoLoginBackServlet?method=login" ; 
			response.sendRedirect(path);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException
	{
		this.logger.info("过滤器初始化了");
	}


}
